<?php
function fn_isNull($sSource,$sReplace="") {
	if (is_null($sSource) || $sSource=="")
		return $sReplace;
	else
		return $sSource;
}

/**change return/enter character to <br/>*/
function fn_nl2br($sValue) {
	$sValue = str_replace(chr(13),""   ,$sValue);
	$sValue = str_replace(chr(10),"<br/>",$sValue);
	return $sValue;
}
/**change <br/> to return/enter character*/
function fn_br2nl($sValue) {
	$sValue = str_replace("<br/>","\n",$sValue);
	return $sValue;
}

function fn_getParam($sSource,$sReplace="") {
	$sValue="";
	if (isset($_GET[$sSource])) {
		$sValue = $_GET[$sSource];
	} else if (isset($_POST[$sSource])) {
		$sValue = $_POST[$sSource];
	}
	if ($sValue=="" or is_null($sValue)) {
		return $sReplace;
	} else {
		$sValue = str_replace("'" ,"&#039;", $sValue);
		$sValue = str_replace("\"","&quot;", $sValue);

		return $sValue;
	}
}
//在执行SQL之前，最好调用此函数，将单引号和双引号常量替换掉
function fn_getParam1($sSource,$sReplace="") {
    if (is_null($sSource)) {
        return $sReplace;
    } else {
        $sSource = str_replace("&#039;", "\'", $sSource);
        $sSource = str_replace("&quot;", "\"", $sSource);
        return $sSource;
    }
}

function fn_setParam($sValue,$sReplace="") {
	if (is_null($sValue) || $sValue=="") {
		return $sReplace;
	} else {
		//$sValue = str_replace("&" ,"&amp;" ,$sValue);
		$sValue = str_replace("'" ,"&#039;",$sValue);
		$sValue = str_replace("\"","&quot;",$sValue);
		$sValue = str_replace("<" ,"&lt;"  ,$sValue);
		$sValue = str_replace(">" ,"&gt;"  ,$sValue);
		$sValue = str_replace("\\","&#92;" ,$sValue);

		return $sValue;
	}
}
////use in development time
function _getInputElement() {
	if (isset($_POST)) {
		$aInput = $_POST;
   	} else if (isset($_GET)) {
		$aInput = $_GET;
	} else {
	    return false;
	}
	$str1 = "";
	$leng = 0;
	foreach ($aInput as $sKey => $sValue) {
	   if (strlen($sKey) > $leng) $leng = strlen($sKey);
	}
    foreach ($aInput as $sKey => $sValue) {
        $str1 = '$'.(substr($sKey,0,3)=="str"?'s'.substr($sKey,3):$sKey);
        $str1 = str_pad($str1,$leng+1);
        $str .= $str1.'= fn_getParam("'.$sKey.'","");'."\n";
    }
    echo $str;
}

function fn_getCurrencyFormat($sPrefix='',$iCurrency,$iDecimalLength=0) {
	return $sPrefix.number_format($iCurrency,$iDecimalLength);
}

function fn_setCurrencyFormat($sCurrency) {
	$temp = str_replace(",","",$sCurrency);
	$sCur = "";
	for ($i=0; $i<strlen($temp); $i++) {
		$ss = substr($temp,$i,1);
		if ($ss>="0" && $ss<="9" || $ss=="." || $ss=="-") $sCur .= $ss;
	}

	return trim($sCur);
}
/**分頁函数
 *@param  $iCurrentPage     当前頁
 *@param  $iRowCount        総記録数
 *@param  $iPageSize        頁面大小
 */
function fn_getPageInfo($iCurrentPage,$iRowCount,$iPageSize)
{
    global $DICT;
    $sPageInfo = "<span class='pagebox'>";

    if ($iCurrentPage>1)
    {
        $sPageInfo.="<span class='pagebox_num'><a href='javascript:page.go(1);'>|<<</a></span>";
    }
    else
    {
        $sPageInfo.="<span class='pagebox_last'>|<<</span>";
    }

    $iPageCount = ceil($iRowCount/$iPageSize);
    $iPageStart = $iCurrentPage;
    $iPageEnd   = $iCurrentPage;
    for ($i=1; $i<=10; $i++)
    {
        if ($iPageStart>1) $iPageStart--;
        if ($iPageEnd < $iPageCount) $iPageEnd++;
        if ($iPageEnd-$iPageStart>8) break;
    }

    for ($i=$iPageStart; $i<=$iPageEnd; $i++)
    {
        if ($i == $iCurrentPage)
        {
            $sPageInfo.="<span class='pagebox_current'>".$iCurrentPage."</span>";
        }
        else if ($i>=1 && $i<=$iPageCount)
        {
            $sPageInfo.="<span class='pagebox_num'><a href='javascript:page.go($i);'>$i</a></span>";
        }
    }

    if ($iCurrentPage < $iPageCount)
    {
        $sPageInfo.="<span class='pagebox_num'><a href='javascript:page.go(".$iPageCount.");'>>>|</a></span>";
    }
    else
    {
        $sPageInfo.="<span class='pagebox_last'>>>|</span>";
    }

    //$sPageInfo.=" &nbsp; ";
    $sPageInfo.=" (总共：<font color='blue'>".$iRowCount."</font>行，";
    $sPageInfo.="<font color='red'>".$iPageCount."</font>页) 每页：";
    $sPageInfo.='<select name="strPageSize" size="1" onchange="document.forms[0].submit();">';
    $sPageInfo.=$DICT->DictOptionList("BASE_PAGELINK",$iPageSize,false);
    $sPageInfo.="</select>";
    $sPageInfo.="行&nbsp;</span>";

    return $sPageInfo;
}

function fn_log($value) {
    global $CFG_REAL_ROOT;
	$handle = fopen($CFG_REAL_ROOT ."/logs/" .date("Y-m-d") .".log", "a");
	if (!$handle) return false;

	$ip_address = $_SERVER['REMOTE_ADDR'];
	$user_name  = isset($_SESSION['cfg_user_name']) ? $_SESSION['cfg_user_name']: '[nobody]';
	fwrite($handle, date('H:i:s ') .$ip_address .' ' .$user_name .' ' .$value ."\n");
	fclose($handle);
}

function fn_getSysTime() {
	$time = date("YmdGis");
	$rand = rand(100,999);
	return $time.$rand;
}

function fn_fmDatetime($sDateTime,$sReplace="") {
	if ($sDateTime=="0000-00-00" || $sDateTime=="00:00" ||
        $sDateTime=="0000-00-00 00:00" || $sDateTime=="0000-00-00 00:00:00"){
		return $sReplace;
	} else {
		//$sDate = strtotime($sDateTime);
		//return (date("H:i:s",$sDate)=="00:00:00"?date("Y-m-d",$sDate):date("Y-m-d H:i",$sDate));
        return $sDateTime;
	}
}

function fn_echoRedStar($params)
{
	echo "<font class='redstar'>★</font>";
}
$SM->register_function("getRedStar","fn_echoRedStar");

function fn_getErrMesg($params) {
    if (isset($params["ErrMesg"]) && $params["ErrMesg"]!="") {
        $message = "\n alert(\"".$params["ErrMesg"]."\");\n";
    } else {
        $message = "";
    }
    return $message;
}
$SM->register_function("getErrMesg","fn_getErrMesg");

/**获取当前登录的用户与会员之间的关系
 * $sUsername 当前Login用户
 * $sMember浏览的网站成员用户名
 *@return 1,同一单位成员；2,好友；3,同一单位成员 and 好友; 4,毫无关系
 */
function fn_getMemberRelation($sCorpCD,$sUsername,$sMember) {
    global $DB,$TABLE_EP_USER,$TABLE_FRIEND;

    $flag = 0;
    $sql = "SELECT COUNT(ID) FROM ".$TABLE_EP_USER;
    $sql.= " WHERE EPID='".$sCorpCD."' AND (USERNAME='".$sUsername."'";
    $sql.= " OR USERNAME='".$sMember."')";
    $rowCount = $DB->getFirstField($sql);
    if ($rowCount == 2) {
        $flag = 1; //同一单位成员
    }
    $sql = "SELECT COUNT(ID) FROM ".$TABLE_FRIEND;
    $sql.= " WHERE EPID='".$sCorpCD."' AND USERNAME='".$sUsername."'";
    $sql.= " AND USER_NAME='".$sMember."'";
    $rowCount = $DB->getFirstField($sql);
    if ($rowCount >= 1) {
        if ($flag == 1)
            return 3; //同一单位成员 and 好友
        else
            return 2; //好友only
    } else {
        if ($flag == 1)
            return 1; //同一单位成员only
        else
            return 4; //
    }
}
/**通过设置的SECRUITY信息获取是否有查看的权限b
 *@RETURN TRUE--有查看的权限；FALSE--没有查看的权限
 */
function fn_getSecViewRight($sSecurity,$sRelation) {
    if ($sSecurity=='5') {
        return false;  //完全保密
    } else if ($sSecurity=='1' ||  //全员开放
               $sSecurity=='2' && ($sRelation==1 || $sRelation==2  || $sRelation==3) ||  //本单位和好友开放
               $sSecurity=='3' && ($sRelation==1 || $sRelation==3) ||  //本单位内开放
               $sSecurity=='4' && ($sRelation==2 || $sRelation==3)) {  //只对好友开放
        return true;
    } else {
        return false;
    }
}

function fn_getUserPetname($sEPID,$sUsername,$isShowTruename=true) {
    global $TABLE_EP_USER,$DB;

    if ($isShowTruename) {
        $sql = "SELECT IFNULL(PETNAME,TRUENAME) AS SENDER ";
    } else {
        $sql = "SELECT PETNAME ";
    }
    $sql.= "FROM $TABLE_EP_USER ";
    $sql.= "WHERE EPID='".$sEPID."' AND USERNAME='".$sUsername."'";
    return $DB->getFirstField($sql);
}
?>